package com.loginlib.table.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.TextView;


import com.loginlib.R;
import com.wanhe.base.model.disgn.OrderedBill;
import com.wanhe.base.model.disgn.TableBillModel;

import java.util.ArrayList;

/**
 * Created by XieZaiWei on 2017/4/26.
 */

public class TableBillAdapter2 extends BaseExpandableListAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    private ArrayList<OrderedBill> mArrOrderedBill;

    public TableBillAdapter2(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(ArrayList<OrderedBill> arrOrderedBill) {
        if(mArrOrderedBill != null){
            mArrOrderedBill.clear();
            mArrOrderedBill = null;
        }
        mArrOrderedBill = arrOrderedBill;
        notifyDataSetChanged();
    }

    public void clean(){
        if(mArrOrderedBill != null){
            mArrOrderedBill.clear();
            mArrOrderedBill = null;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if(mArrOrderedBill == null){
            return 0;
        }
        return mArrOrderedBill.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public OrderedBill getGroup(int groupPosition) {
        return mArrOrderedBill.get(groupPosition);
    }

    @Override
    public TableBillModel getChild(int groupPosition, int childPosition) {
        return mArrOrderedBill.get(groupPosition).getArrTableBill().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = mLayoutInflater.inflate(R.layout.cart_group, null);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);

        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.tv_title.setText(mArrOrderedBill.get(groupPosition).getTypeName());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = mLayoutInflater.inflate(R.layout.cart_child_grid, null);
            holder.gv_dish = (GridView) convertView.findViewById(R.id.gv_dish);
            convertView.setTag(holder);

        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        holder.gv_dish.setAdapter(new TableBillChildAdapter(mContext, mArrOrderedBill.get(groupPosition).getArrTableBill()));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    private static class GroupHolder {
        public TextView tv_title;
    }

    private static class ChildHolder {
        public GridView gv_dish;

    }
}
